import { GetTopiclList } from "../../request/api";

// type
const SET_TOPIC_LOADING = 'topic/setLoading';
const SET_TOPIC_DATA = 'topic/setData';

// 初始state
const initialState = {
  loading: false,
  data: []
}

// 同步事件
const setTopicData = (data) => ({
  type: SET_TOPIC_DATA,
  data
})

// 异步事件
export const requestTopicData = () => (dispatch) => {
  GetTopiclList({ page: 1, size: 10 }).then((res) => {
    let { data } = res.data.data;
    dispatch(setTopicData(data));
    // dispatch({ type: 'topic/setLoading', loading: false });
  });
};


const reducer = (state = initialState, action) => {
  switch (action.type) {
    case SET_TOPIC_DATA:
      return {
        ...state,
        data: action.data,
      }
    case SET_TOPIC_LOADING:
      return {
        ...state,
        loading: action.loading,
      }
    default:
      return state;
  }

};

export default reducer;